package com.exam.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class ValidateFile {

	private static Properties properties = new Properties();
	static{
		try {
			properties.load(ValidateFile.class.getClassLoader().getResourceAsStream("arrowuploadfiletype.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * ��֤�ϴ��ļ������Ƿ�����ͼƬ��ʽ
	 * @param file �ļ�
	 * @param contentType �ļ�����
	 * @param fileName �ļ���
	 * @return
	 */
	public static boolean validateImageFileType(File file,String contentType,String fileName){
		if(file!=null && file.length()>0){
			//����������֤�ļ���չ���͸�ȫ
			List<String> arrowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpg","image/jpeg","image/pjpeg");
			List<String> arrowExtension = Arrays.asList("gif","jpg","bmp","png");
			String extension=getExtension(fileName);
			return arrowType.contains(contentType.toLowerCase()) && arrowExtension.contains(extension);
		}
		return true;
	}
	
	/**
	 * ��ȡ�ļ��ĺ�׺��
	 * @param fileName �ļ���
	 * @return
	 */
	public static String getExtension(String fileName){
		return fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
	}
	
	/**
	 * ��֤�ϴ��ļ��Ƿ�����ͼƬ/flash����/word�ļ�/exe�ļ�/pdf�ļ�/TxT�ļ�/xls�ļ�/ppt�ļ�
	 * @param formfile
	 * @return
	 */
	public static boolean validateFileType(File file,String contentType,String fileName){
		if(file!=null && file.length()>0){
			String ext = getExtension(fileName);
			List<String> arrowType = new ArrayList<String>();
			for(Object key : properties.keySet()){
				String value = (String)properties.get(key);
				String[] values = value.split(",");
				for(String v : values){
					arrowType.add(v.trim());
				}
			}
			return arrowType.contains(contentType.toLowerCase()) && properties.keySet().contains(ext);
		}
		return true;
	}
}
